package com.vip.oauth2.security;

import com.vip.database.dto.perm.GetPermDTO;
import com.vip.database.dto.user.GetUserDTO;
import com.vip.database.mapper.SysUserMapper;
import com.vip.oauth2resource.util.CustomUserDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 安全配置适配器：Security核心配置
 *
 * @author wgb
 */
@Slf4j
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private SysUserMapper userMapper;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return username -> {
            GetUserDTO user = userMapper.findUserByAccountWithPerm(username);
            if (user != null) {
                CustomUserDetail customUserDetail = new CustomUserDetail();
                customUserDetail.setUsername(user.getAccount());
                customUserDetail.setId(user.getId());
                customUserDetail.setName(user.getName());
                customUserDetail.setPassword("{bcrypt}" + bCryptPasswordEncoder.encode(user.getPassword()));
                List<GetPermDTO> perms = user.getPerms();
                int size = perms.size();
                String[] permsArr = new String[size];
                for (int i = 0; i < size; i++) {
                    permsArr[i] = perms.get(i).getCode();
                }
                List<GrantedAuthority> list = AuthorityUtils.createAuthorityList(permsArr);
                customUserDetail.setAuthorities(list);
                return customUserDetail;
            } else {
                return null;
            }
        };
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }
}
